package org.devocean.groupbuying.web.backend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.domain.Menu;
import org.devocean.groupbuying.service.MenuService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = { "/gb-cpanel/menus" })
public class MenusController {

	@Autowired
	private MenuService menuService;
	
	/**
	 * List all menus for a tree view 
	 */

	@RequestMapping(value = "/tree")
	public Map<String, ? extends Object> tree(
			@RequestParam(value = "node", required = false) String node) {
		try {
			
			if(node != null){
			if (node.equals("root")) {
				List<Menu> menus = new ArrayList<Menu>();
				menus = menuService.findAllMenus();
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				int i = 0;
				for (Menu menu : menus) {
					if (menu.getMenu() == null) {
						Map<String, Object> data = new ConcurrentHashMap<String, Object>();
						data.put("id", menu.getMenuID());
						data.put("text", menu.getTitle());
						data.put("title", menu.getTitle());
						data.put("lang", menu.getLang());
						data.put("ML_id", menu.getMenuLocalization().getMenuLocalizationID());
						data.put("level", i);
						if (menu.getMenus().size() > 0)
							data.put("leaf", false);
						else
							data.put("leaf", true);

						result.add(data);
					}
					i++;
				}
				return RenderMapUtils.getMapChildren(result);
			} else {
				Menu root = menuService.findMenuById(Integer.parseInt(node));
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				int i = 0;
				for (Menu menu : root.getMenus()) {
					Map<String, Object> data = new ConcurrentHashMap<String, Object>();
					data.put("id", menu.getMenuID());
					data.put("text", menu.getTitle());
					data.put("title", menu.getTitle());
					data.put("lang", menu.getLang());
					data.put("ML_id", menu.getMenuLocalization().getMenuLocalizationID());
					data.put("level", i);
					if (menu.getMenus().size() > 0)
						data.put("leaf", false);
					else
						data.put("leaf", true);

					result.add(data);
					i++;
				}
				return RenderMapUtils.getMapChildren(result);
			}
			}
			else{
				return RenderMapUtils.getModelMapError("Error retrieving Menus from database.");
			}

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils
					.getModelMapError("Error retrieving Menus from database.");
		}
	}

	/**
	 * List all menus
	 */
	@RequestMapping(value = "/all/")
	public Map<String, ? extends Object> list() {
		try {
			List<Menu> menus = menuService.findAllMenus();
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			for (Menu menu : menus) {
				Map<String, Object> data = new ConcurrentHashMap<String, Object>();
				data.put("id", menu.getMenuID());
				data.put("title", menu.getTitle());
				data.put("menu.id", menu.getMenu() == null ? 0 : menu.getMenu()
						.getMenuID());
				result.add(data);
			}
			return RenderMapUtils.getMap(result);

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils
					.getModelMapError("Error retrieving Menus from database.");
		}
	}

	/**
	 * Delete a menu
	 */
	
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable("id") Integer id) {
		menuService.deleteMenu(id);
	}

}
